/*
 * Filename    : SceneGameplay.java
 * Projectname : CobaScene
 * Date        : Jan 1, 2013 , 9:26:28 PM 
 * 
 */

package com.example.cobascene.scene;

import org.andengine.entity.scene.IOnSceneTouchListener;
import org.andengine.entity.scene.Scene;
import org.andengine.entity.scene.background.Background;
import org.andengine.entity.sprite.Sprite;
import org.andengine.input.touch.TouchEvent;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.andengine.opengl.texture.region.TextureRegion;
import org.andengine.ui.activity.BaseGameActivity;
import org.andengine.util.color.Color;

import com.example.cobascene.IManageableScene;
import com.example.cobascene.Utils;
import com.example.cobascene.config.Game_Data;
import com.example.cobascene.config.Game_Define;
import com.example.cobascene.data.Graphic_Data;
import com.example.cobascene.entity.Shuriken;

public class SceneGameplay extends Scene implements IManageableScene, Game_Data, Game_Define, Graphic_Data,
		IOnSceneTouchListener
{
	private BaseGameActivity	activity;
	private BitmapTextureAtlas	texPlayer, texShuriken, texEnemy;
	private TextureRegion		regPlayer, regShuriken, regEnemy;
	private Sprite				sprPlayer, sprShuriken, sprEnemy;

	private float				move	= Utils.getRatio(5);

	public SceneGameplay(BaseGameActivity pActivity)
	{
		// TODO Auto-generated constructor stub
		activity = pActivity;
		loadResources();
		applyResources();
		registerUpdateHandler();
	}

	@Override
	public void loadResources()
	{
		// TODO Auto-generated method stub
		texPlayer = new BitmapTextureAtlas(activity.getTextureManager(), PLAYER_TEX_WIDTH, PLAYER_TEX_HEIGHT,
				Utils.getTextureOption());
		regPlayer = BitmapTextureAtlasTextureRegionFactory.createFromAsset(texPlayer, activity.getAssets(), PLAYER, 0,
				0);
		texShuriken = new BitmapTextureAtlas(activity.getTextureManager(), SHURIKEN_TEX_WIDTH, SHURIKEN_TEX_HEIGHT,
				Utils.getTextureOption());
		regShuriken = BitmapTextureAtlasTextureRegionFactory.createFromAsset(texShuriken, activity.getAssets(),
				SHURIKEN, 0, 0);
		texEnemy = new BitmapTextureAtlas(activity.getTextureManager(), ENEMY_TEX_WIDTH, ENEMY_TEX_HEIGHT,
				Utils.getTextureOption());
		regEnemy = BitmapTextureAtlasTextureRegionFactory.createFromAsset(texEnemy, activity.getAssets(), ENEMY, 0, 0);

		texShuriken.load();
		texPlayer.load();
		texEnemy.load();

	}

	@Override
	public void applyResources()
	{
		// TODO Auto-generated method stub
		setBackground(new Background(Color.WHITE));

		sprPlayer = new Sprite(0, 0, Utils.getRatioW(PLAYER_WIDTH), Utils.getRatioH(PLAYER_HEIGHT), regPlayer,
				activity.getVertexBufferObjectManager());
		sprPlayer.setPosition(Utils.getRatio(50), RES_HEIGHT / 2 - sprPlayer.getHeight() / 2);
		attachChild(sprPlayer);
	}

	@Override
	public void registerUpdateHandler()
	{
		// TODO Auto-generated method stub
		setOnSceneTouchListener(this);

	}

	@Override
	public void unloadResources()
	{
		// TODO Auto-generated method stub

	}

	@Override
	public boolean onSceneTouchEvent(Scene pScene, TouchEvent pSceneTouchEvent)
	{
		// TODO Auto-generated method stub
		// naik
		Utils.TRACE("y - > " + pSceneTouchEvent.getY());
		if (pSceneTouchEvent.getX() < RES_WIDTH / 2)
		{
			if (pSceneTouchEvent.getY() < sprPlayer.getY())
			{
				sprPlayer.setY(sprPlayer.getY() - move);
			} else if (pSceneTouchEvent.getY() > sprPlayer.getY())
			{
				sprPlayer.setY(sprPlayer.getY() + move);
			}
		} else if (pSceneTouchEvent.getX() > RES_WIDTH / 2)
		{
			shoot();
		}

		return false;
	}

	private void shoot()
	{
		// TODO Auto-generated method stub
		Shuriken s = new Shuriken(this);

	}

}
